package com.we.weblog.web.controller;

import com.we.weblog.domain.Game;
import com.we.weblog.domain.ResultVo;
import com.we.weblog.service.GameService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
@RequestMapping("/game")
public class GameController{
    @Autowired
    private GameService gameService;
    
    @RequestMapping("/addGame")
    public ResultVo addGame(@RequestBody Game game) {
        int result = gameService.addGame(game);
        if(result>0) {
            return ResultVo.success(result,"添加成功");
        } else  {
            return ResultVo.fail("添加失败");
        }
    }

    @RequestMapping("/updateGameById")
    public ResultVo updateGameById(@RequestBody Game game) {
        int result = gameService.updateGameById(game);
        if(result>0) {
            return ResultVo.success(result,"修改成功");
        } else  {
            return ResultVo.fail("修改失败");
        }
    }

    @RequestMapping("/deleteGameById")
    public ResultVo deleteGameById(@RequestBody Game game) {
        int result = gameService.deleteGameById(game.getGameId());
        if(result>0) {
            return ResultVo.success(result,"删除成功");
        } else  {
            return ResultVo.fail("删除失败");
        }
    }

    @RequestMapping("/selectGameId")
    public ResultVo selectGameId(@RequestBody Game game) {
        Game resultGame = gameService.selectGameId(game.getGameId());
        if(resultGame != null) {
            return ResultVo.success(resultGame,"查询成功");
        } else  {
            return ResultVo.fail("查询失败");
        }
    }

    @RequestMapping("/selectGame")
    public ResultVo selectGame() {
        List<Game> games = gameService.selectGame();
        if(games == null) {
            return ResultVo.fail("查询失败");
        }
        if(games.size() == 0) {
            return ResultVo.success("查询成功，没有找到对应数据");
        }
        return ResultVo.success(games, "查询成功");
    }

    @RequestMapping("/selectGameByCategory")
    public ResultVo selectGameByCategory(@RequestBody Game game) {
        List<Game> games = gameService.selectGameByCategory(game.getCategoryId());
        if(games == null) {
            return ResultVo.fail("查询失败");
        }
        if(games.size() == 0) {
            return ResultVo.success("查询成功，没有找到对应数据");
        }
        return ResultVo.success(games,"查询成功");
    }

    @RequestMapping("/selectGameByTag")
    public ResultVo selectGameByTag(@RequestBody Game game) {
        List<Game> games = gameService.selectGameByTag(game.getTagId());
        if(games == null) {
            return ResultVo.fail("查询失败");
        }
        if(games.size() == 0) {
            return ResultVo.success("查询成功，没有找到对应数据");
        }
        return ResultVo.success(games,"查询成功");
    }
}
